package com.bbx.taxi.client.widget.tagview;



import java.util.ArrayList;
import java.util.List;

import com.bbx.taxi.official.R;

import android.animation.LayoutTransition;
import android.content.Context;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;

/**
 * @author kince
 * @categorytagview
 * 
 */
public class TagListView extends FlowLayout implements OnClickListener {
	/**默认的是否可以checked*/
    private static boolean DEFAULT_CHECKED=false;
	private boolean mIsDeleteMode;
	private OnTagCheckedChangedListener mOnTagCheckedChangedListener;
	private OnTagLongClickListener mOnTagLongClickListener;
	private OnTagClickListener mOnTagClickListener;
	private OnDelClickListener mOnDelClickListener;
	private int mTagViewBackgroundResId;
	private int mTagViewTextColorResId;
	private  List<Tag> mTags = new ArrayList<Tag>();
	private  List<RelativeLayout> mLayouts = new ArrayList<RelativeLayout>();
	private  List<TagView> mTagViews = new ArrayList<TagView>();
	private  List<ImageView> mDelView = new ArrayList<ImageView>();

	/**
	 * @param context
	 */
	public TagListView(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
		init();
	}

	/**
	 * @param context
	 * @param attributeSet
	 */
	public TagListView(Context context, AttributeSet attributeSet) {
		super(context, attributeSet);
		// TODO Auto-generated constructor stub
		init();
	}

	/**
	 * @param context
	 * @param attributeSet
	 * @param defStyle
	 */
	public TagListView(Context context, AttributeSet attributeSet, int defStyle) {
		super(context, attributeSet, defStyle);
		// TODO Auto-generated constructor stub
		init();
	}

	@Override
	public void onClick(View v) {
		if ((v instanceof TagView)) {
			Tag localTag = (Tag) v.getTag();
			if (this.mOnTagClickListener != null) {
				this.mOnTagClickListener.onTagClick((TagView) v, localTag);
			}
		}
	}
	private void init() {
		LayoutTransition transition = new LayoutTransition();
		transition.setAnimator(LayoutTransition.CHANGE_APPEARING,
				transition.getAnimator(LayoutTransition.CHANGE_APPEARING));
		transition.setAnimator(LayoutTransition.APPEARING, transition.getAnimator(LayoutTransition.APPEARING));
		transition.setAnimator(LayoutTransition.DISAPPEARING,
				transition.getAnimator(LayoutTransition.DISAPPEARING));
		transition.setAnimator(LayoutTransition.CHANGE_DISAPPEARING,
				transition.getAnimator(LayoutTransition.CHANGE_DISAPPEARING));
		this.setLayoutTransition(transition);
	}

	private void inflateTagView(final Tag t, boolean b,int index) {
       final RelativeLayout layout=(RelativeLayout) View.inflate(getContext(),
				R.layout.tag, null);
		final TagView localTagView = (TagView) layout.findViewById(R.id.mTagView);
		final ImageView iv_del = (ImageView) layout.findViewById(R.id.iv_del);
		localTagView.setText(t.getTitle());
		localTagView.setTag(t);

		if (mTagViewBackgroundResId <= 0) {
			mTagViewBackgroundResId = R.drawable.tag_bg;
			localTagView.setBackgroundResource(mTagViewBackgroundResId);
		}

		localTagView.setChecked(t.isChecked());
		localTagView.setCheckEnable(b);
		/**特殊按钮不参与删除*/
		if (mIsDeleteMode&&!t.getIsSpecial()) {
			iv_del.setVisibility(View.VISIBLE);
			iv_del.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					if (TagListView.this.mOnDelClickListener != null) {
						TagListView.this.mOnDelClickListener.onDelClick(localTagView, t,iv_del,layout);
					}
				}
			});
		}else{
			iv_del.setVisibility(View.GONE);
			localTagView.setOnClickListener(this);
		}
		if (t.getBackgroundResId() > 0) {
			localTagView.setBackgroundResource(t.getBackgroundResId());
		}
		if ((t.getLeftDrawableResId() > 0) || (t.getRightDrawableResId() > 0)) {
			localTagView.setCompoundDrawablesWithIntrinsicBounds(
					t.getLeftDrawableResId(), 0, t.getRightDrawableResId(), 0);
		}
		localTagView
				.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
					public void onCheckedChanged(
							CompoundButton paramAnonymousCompoundButton,
							boolean paramAnonymousBoolean) {
						t.setChecked(paramAnonymousBoolean);
						if (TagListView.this.mOnTagCheckedChangedListener != null) {
							TagListView.this.mOnTagCheckedChangedListener
									.onTagCheckedChanged(
											(TagView) paramAnonymousCompoundButton,
											t);
						}
					}
				});
		localTagView.setOnLongClickListener(new OnLongClickListener() {
			
			@Override
			public boolean onLongClick(View arg0) {
				if (TagListView.this.mOnTagLongClickListener != null) {
					TagListView.this.mOnTagLongClickListener
							.onTagLongClick((TagView) arg0,t);
					return true;
				}
				return false;
			}
		});
		mTagViews.add(index,localTagView);
		mDelView.add(index,iv_del);
		mLayouts.add(index,layout);
		addView(layout,index);
	}

	public void addTag(int i, String s) {
		addTag(i, s, DEFAULT_CHECKED);
	}

	public void addTag(int i, String s, boolean b) {
		addTag(new Tag(i, s), b);
	}

	public void addTag(Tag tag) {
		addTag(tag, DEFAULT_CHECKED);
	}
	public void addTag(Tag tag,int index) {
		addTag(tag, DEFAULT_CHECKED,index);
	}

	public void addTag(Tag tag, boolean b) {
		mTags.add(tag);
		inflateTagView(tag, b,mTags.size()-1);
	}
	public void addTag(Tag tag, boolean b,int index) {
		mTags.add(index,tag);
		inflateTagView(tag, b,index);
	}

	public void addTags(List<Tag> lists) {
		addTags(lists, DEFAULT_CHECKED);
	}

	public void addTags(List<Tag> lists, boolean b) {
		for (int i = 0; i < lists.size(); i++) {
			addTag((Tag) lists.get(i), b);
		}
	}

	public List<Tag> getTags() {
		return mTags;
	}

	public View getViewByTag(Tag tag) {
		return findViewWithTag(tag);
	}

	public void removeTag(Tag tag,TagView mTagView,ImageView iv_del,RelativeLayout layout) {
		mTags.remove(tag);
		mTagViews.remove(mTagViews);
		mDelView.remove(iv_del);
		removeView(layout);
//		removeView(getViewByTag(tag));
	}

	public void setDeleteMode(boolean b) {
		mIsDeleteMode = b;
		for(int i=0;i<mTags.size();i++){
			final RelativeLayout layout=mLayouts.get(i);
			final TagView mTagView=mTagViews.get(i);
			final ImageView iv_del=mDelView.get(i);
			final Tag mTag=mTags.get(i);
			/**特殊按钮不参与删除*/
			if (mIsDeleteMode&&!mTag.getIsSpecial()) {
				iv_del.setOnClickListener(new OnClickListener() {
					
					@Override
					public void onClick(View v) {
						if (TagListView.this.mOnDelClickListener != null) {
							TagListView.this.mOnDelClickListener.onDelClick(mTagView, mTag,iv_del,layout);
						}
					}
				});
				iv_del.setVisibility(View.VISIBLE);
				mTagView.setOnClickListener(null);
			}else{
				iv_del.setVisibility(View.GONE);
				mTagView.setOnClickListener(this);
				
			}
		}

	}
	public boolean getDeleteMode(){
		return mIsDeleteMode;
	}

	public void setOnTagCheckedChangedListener(
			OnTagCheckedChangedListener onTagCheckedChangedListener) {
		mOnTagCheckedChangedListener = onTagCheckedChangedListener;
	}
	public void setOnTagLongClickListener(OnTagLongClickListener onTagLongClickListener) {
		mOnTagLongClickListener = onTagLongClickListener;
	}
	public void setOnTagClickListener(OnTagClickListener onTagClickListener) {
		mOnTagClickListener = onTagClickListener;
	}
	public void setOnDelClickListener(OnDelClickListener onDelClickListener) {
		mOnDelClickListener = onDelClickListener;
	}

	public void setTagViewBackgroundRes(int res) {
		mTagViewBackgroundResId = res;
	}

	public void setTagViewTextColorRes(int res) {
		mTagViewTextColorResId = res;
	}

	public void setTags(List<? extends Tag> lists) {
		setTags(lists, DEFAULT_CHECKED);
	}

	public void setTags(List<? extends Tag> lists, boolean b) {
		removeAllViews();
		mTags.clear();
		for (int i = 0; i < lists.size(); i++) {
			addTag((Tag) lists.get(i), b);
		}
	}

	public static abstract interface OnTagCheckedChangedListener {
		public abstract void onTagCheckedChanged(TagView tagView, Tag tag);
	}

	public static abstract interface OnTagClickListener {
		public abstract void onTagClick(TagView tagView, Tag tag);
	}
	public static abstract interface OnDelClickListener {
		public abstract void onDelClick(TagView tagView, Tag tag,ImageView iv_del,RelativeLayout layout);
	}
	public static abstract interface OnTagLongClickListener {
		public abstract void onTagLongClick(TagView tagView, Tag tag);
	}

}
